package com.example.doma.service;

import com.example.doma.mapper.studentMapper;
import com.example.doma.pojo.dorm;
import com.example.doma.pojo.student;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class studentService {
    @Autowired
    private studentMapper studentMapper;

    @Autowired
    private dormService dormService;

    public PageInfo<student> getStudent(Integer pageNum ,Integer pagesize)
    {
        PageHelper.startPage(pageNum,pagesize);
        List<student> students = studentMapper.getstudents();
        PageInfo<student> studentPageInfo = new PageInfo<>(students, pagesize);
        if(studentMapper.getpages()%pagesize>0)
            studentPageInfo.setNavigatePages(Integer.valueOf(studentMapper.getpages()/pagesize+1));
        else
            studentPageInfo.setNavigatePages(Integer.valueOf(studentMapper.getpages()/pagesize));

        return studentPageInfo;
    }
    public void addStudent(Map<String,Object> map)
    {
        studentMapper.add(map);
    }
    public  student getstudentByid(int id)
    {
        return studentMapper.getstudentByid(id);
    }

    public  int issex(char sex)
    {
        if(sex=='男')
            return 1;
        else
            return 0;
    }
    //更新学生信息
    public  void  update(student student)
    {
        studentMapper.update(student);
    }
    //删除学生信息
      public  int deletestudent(int id)
      {
          studentMapper.deleteByid(id);
          return 1;
      }
      //判断是否存在该学生
      public  boolean isstudent(int id)
      {
          if(studentMapper.getstudentByid(id)==null)
              return false;
          else
              return true;
      }
      //通过寝室·搜索学生
      public List<student> getstudentsBydorm(String apart_num ,int dormi_num)
      {
          System.out.println("收到查看寝室成员信息参数"+apart_num+dormi_num);
          List<student> students = studentMapper.getstudentBydorm(apart_num, dormi_num);
          System.out.println(students);
          return students;
      }

    //根据学生id获取推荐宿舍
    public List<Map<String,Object>>  getdoormsById(int id)
    {
        student st = studentMapper.getstudentByid(id);

         Map<String,String> temp=new HashMap<>();
        //获取同班同学有空余寝室
        List<dorm> dorms_class = dormService.getdormsByspec_nameAndclass_nameAndgrade(st.getSpec_name(), st.getClass_name(), st.getGrade(), st.getSex());
        //获取同专业同学空余寝室
        List<dorm> dorms_spec = dormService.getdormsByspec_nameAndgrade(st.getSpec_name(), st.getGrade(), st.getSex(),st.getClass_name());
        //获取同院级学生
        List<dorm> dorms_facul = dormService.getdormsByfaculty_nameAndBygrade(st.getFaculty_name(), st.getGrade(), st.getSex(),st.getSpec_name());

        //所有宿舍打包
        List<Map<String,Object>> dorms=new ArrayList<>();
      for (dorm dd:dorms_class)
      {

          if (temp.get(dd.getApart_num()+String.valueOf(dd.getDormi_num()))==null)
          {
              temp.put(dd.getApart_num()+String.valueOf(dd.getDormi_num()),"true");
              int[] bednums_class = dormService.getBednum(dd.getApart_num(), dd.getDormi_num());
              for (int i=1;i<bednums_class.length;i++)
              {
                  if (bednums_class[i]==0)
                  {
                      Map<String,Object> dor=new HashMap<>();
                      dor.put("apart_num",dd.getApart_num());
                      dor.put("dormi_num",dd.getDormi_num());
                      dor.put("text",dd.getApart_num()+" "+dd.getDormi_num()+"("+i+"号床）"+"(同班)");
                      dorms.add(dor);
                  }
              }
          }


      }
      for (dorm dd:dorms_spec)
      {
          if (temp.get(dd.getApart_num()+String.valueOf(dd.getDormi_num()))==null) {
              temp.put(dd.getApart_num() + String.valueOf(dd.getDormi_num()), "true");
              int[] bednums_spec = dormService.getBednum(dd.getApart_num(), dd.getDormi_num());
              for (int i = 1; i < bednums_spec.length; i++) {
                  if (bednums_spec[i] == 0) {
                      Map<String, Object> dor = new HashMap<>();
                      dor.put("apart_num", dd.getApart_num());
                      dor.put("dormi_num", dd.getDormi_num());
                      dor.put("text", dd.getApart_num() + " " + dd.getDormi_num() + "(" + i + "号床）" + "(同专业)");
                      dorms.add(dor);
                  }
              }
          }

      }
      for (dorm dd:dorms_facul)
      {
          if (temp.get(dd.getApart_num()+String.valueOf(dd.getDormi_num()))==null) {
              temp.put(dd.getApart_num() + String.valueOf(dd.getDormi_num()), "true");
              int[] bednums_facul = dormService.getBednum(dd.getApart_num(), dd.getDormi_num());
              for (int i = 1; i < bednums_facul.length; i++) {
                  if (bednums_facul[i] == 0) {
                      Map<String, Object> dor = new HashMap<>();
                      dor.put("apart_num", dd.getApart_num());
                      dor.put("dormi_num", dd.getDormi_num());
                      dor.put("text", dd.getApart_num() + " " + dd.getDormi_num() + "(" + i + "号床）" + "(同院)");
                      dorms.add(dor);
                  }
              }
          }

      }
        System.out.println(dorms);
    return dorms;
    }



    //调寝功能中从返回值中获取参数
    public Map<String,Object> getApart_numAndDormi_numAndbed_num(String dd)
    {
        Map<String,Object> Doma = new HashMap();
        Doma.put("apart_num",dd.substring(0, dd.indexOf(' ')));
        Doma.put("dormi_num",Integer.valueOf(dd.substring( dd.indexOf(' ')+1,dd.indexOf('('))));
        Doma.put("bed_num",Integer.valueOf(dd.substring(dd.indexOf('(')+1,dd.indexOf('号'))));
        return Doma;
    }
    //调寝功能
    public void   setDorm(int id,String apart_num,int dormi_num,int bed_num)
    {
        //获取学生信息，修改原来所在寝室信息
        student student = studentMapper.getstudentByid(id);
        List<dorm> dorm_1 = dormService.getDormByapart_numAnddormi_num(student.getApart_num(), student.getDormi_num());
        if (dorm_1!=null)
        {
            //修改原来宿舍
            dorm dorm = dorm_1.get(0);
            System.out.println("修改寝室--原来寝室："+dorm);
             dorm.setNum(dorm.getNum()+1);
            System.out.println("修改原来寝室后--"+dorm);
            dormService.updatedorm(dorm);
            System.out.println("修改原来寝室成功");
        }
     //修改现在宿舍
        student.setApart_num(apart_num);
        student.setDormi_num(dormi_num);
        student.setBed_num(bed_num);
        studentMapper.update(student);
        dorm dorm_2 = dormService.getdrom(apart_num, dormi_num);
        dorm_2.setNum(dorm_2.getNum()+1);
        System.out.println(id+"学生调寝成功");


    }

    //批量添加学生功能
    public void  AddStudents(HSSFWorkbook workbook)
    {
        //获取工作表
        HSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        System.out.println("获取学生信息：");
        List<student> students=new ArrayList<>();
        System.out.println("学生参数：");
        //学生参数
        HSSFRow row1 = sheet.getRow(0);
        for (int i=0;i<row1.getLastCellNum();i++)
        {
            HSSFCell cell = row1.getCell(i);
            cell.setCellType(CellType.STRING);
            System.out.print(cell.getStringCellValue()+"  ");
        }
        System.out.println();
        List<String> dor =new ArrayList<>();
        for (int i=1;i<lastRowNum;i++)
        {
            //获取指定行
            HSSFRow row = sheet.getRow(i);
            if (row==null)
                continue;

            for (int j=0;j<row.getLastCellNum();j++)
            {

                HSSFCell cell = row.getCell(j);
                if(cell==null)
                    continue;
                cell.setCellType(CellType.STRING);
                dor.add(j,cell.getStringCellValue());
                System.out.print(cell.getStringCellValue()+"  ");

            }
            students.add(new student(Integer.valueOf(dor.get(0)),dor.get(1),issex(dor.get(2).charAt(0)),dor.get(3),dor.get(4),Integer.valueOf(dor.get(5)),dor.get(6),dor.get(7),Integer.valueOf(dor.get(8)),Integer.valueOf(dor.get(9)),dor.get(10)));

            System.out.println("");
        }
        System.out.println(students);
        studentMapper.adds(students);
    }


    public List<student> getstudents()
    {
        List<student> students = studentMapper.getstudents();
        return  students;
    }

}



